<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class RandomNameGenerator {


    
    public static function uniqueFilename()
    {
            $arrIp = explode('.', $_SERVER['REMOTE_ADDR']);
            list($usec, $sec) = explode(' ', microtime());
            $usec = (integer) ($usec * 65536);
            $sec = ((integer) $sec) & 0xFFFF;
            $strUid = sprintf("%08x-%04x-%04x", ($arrIp[0] << 24) | ($arrIp[1] << 16) | ($arrIp[2] << 8) | $arrIp[3], $sec, $usec);
            return $strUid;
    }

    public static function transactionCode() {
        $length = 16;
        $trans_code = date('Y');
        # generate digits
        while ( strlen($trans_code) < ($length - 1) ) {
            $trans_code .= rand(0,9);
        }
        # Calculate sum
        $sum = 0;
        $pos = 0;
        $reversed_trans_code = strrev( $trans_code );
        while ( $pos < $length - 1 ) {
            $odd = $reversed_trans_code[ $pos ] * 2;
            if ( $odd > 9 ) {
                $odd -= 9;
            }
            $sum += $odd;
            if ( $pos != ($length - 2) ) {
                $sum += $reversed_trans_code[ $pos +1 ];
            }
            $pos += 2;
        }

        # Calculate check digit
        $checkdigit = (( floor($sum/10) + 1) * 10 - $sum) % 10;
        $trans_code .= $checkdigit;

        return sprintf("%05d-%04d-%04d-%04d", substr($trans_code, 0,5), substr($trans_code, 5,4), substr($trans_code, 9,4), substr($trans_code, 13,3));
    }
}

?>
